﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Text;

public partial class Web_Back_OrderManage : System.Web.UI.Page
{

    //用来生成用户列表的html标签
    public string OrderDetailList { get; set; }
    //实例化一个OrderDao
    //Dao数据库访问操作
    public static OrderDao orderDao = new OrderDao();
    public static StateDao stateDao = new StateDao();

    protected void Page_Load(object sender, EventArgs e)
    {
        //在Session中取admin对象
        Account admin = (Account)Session["admin"];
        //判断是否已经登录
        if (admin == null)
        {
            Response.Redirect("../Login.aspx");
        }
        else
        {
            //刷新列表
            UpdateList();
        }
    }

    private void UpdateList()
    {
        //通过orderDao对象取出所有的订单的信息
        List<Order> list = orderDao.GetAllOrders();
        ReflashTable(list);
    }

    // 删除图书
    protected void BtnDeleteOrder_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(OrderId.Text);
        Console.WriteLine(id);
        //调用删除方法删除图书
        if (orderDao.DeleteOrderById(id))
        {
            //刷新当前页 0为立即刷新
            Response.AddHeader("Refresh", "0");
        }
    }


    protected void ButtonUnpaid_Click(object sender, EventArgs e)
    {
        OrderSate.Text = "1";
        //通过orderDao对象取出所有的订单的信息
        List<Order> list = orderDao.GetOrdersByState(1);
        ReflashTable(list);
    }

    protected void ButtonDelivered_Click(object sender, EventArgs e)
    {
        OrderSate.Text = "2";
        //通过orderDao对象取出所有的订单的信息
        List<Order> list = orderDao.GetOrdersByState(2);
        ReflashTable(list);
    }

    protected void ButtonWaitReceive_Click(object sender, EventArgs e)
    {
        OrderSate.Text = "3";
        List<Order> list = orderDao.GetOrdersByState(4);
        ReflashTable(list);
    }

    protected void ButtonFinished_Click(object sender, EventArgs e)
    {
        OrderSate.Text = "4";
        //通过orderDao对象取出所有的订单的信息
        List<Order> list = orderDao.GetOrdersByState(5);
        ReflashTable(list);
    }


    protected void ReflashTable(List<Order> list)
    {
        List<State> orderStates = stateDao.GetOrderState();
        string stateOption="";
        foreach (State state in orderStates)
        {
            stateOption= stateOption+"<option value = '"+ state.Id+ "' >"+state.StateName+"</option >";
        }

        StringBuilder sb = new StringBuilder();

        if (list != null)
        {
            //遍历列表
            foreach (Order order in list)
            {
                //生成html
                sb.AppendFormat(
                 "<tr class='odd gradeX'>" +
                "<td>{0}</td><td>{1}</td><td>{2}</td>" +
                "<td style='width:20%;'><img width='50%' src='{3}'/></td>" +
                "<td>{4}</td><td>{5}</td><td>{6}</td>" +
                 "<td><div orderId = '{0}' state = '{7}' class='btn-group-sm text-center'>" +
                 "<button type = 'button' class='btn btn-success btn_update'>更新状态</button><p></p>" +
                 "<select class='selectpicker show-tick'  data-style='btn-primary' name='optionstate'>" + stateOption + "</select></div></td>" +
                "<td><div orderId = '{0}' class='btn-group-sm text-center'>" +
                "<button type = 'button' class='btn btn-danger btn_delete' data-toggle='modal' data-target='#confirm-delete'>删除订单</button></div></td>",
                order.Id, order.User.Username, order.Book.Name, order.Book.Image, order.OrderTime, order.Count, order.Money, order.State);
            }
            //返回给页面
        }
        else {
            sb.AppendFormat("<tr class ='text - center'><td colspan='9' class ='text-center'>还没有相关订单信息哦！</td><tr>");
        }
        OrderDetailList = sb.ToString();
    }

    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(OrderId.Text);
        int state= Convert.ToInt32(NewState.Text);
        //调用更新方法更新图书
        if (orderDao.UpdateOrderById(id,state))
        {
            //刷新当前页 0为立即刷新
            Response.AddHeader("Refresh", "0");
        }
    }
}